const express = require('express');
const cors = require('cors');
const body_parser = require('body-parser');// 解析参数
const nodemailer = require('nodemailer');
const app = express();



// 接口请求，解析参数
app.use(body_parser.json());
app.use(body_parser.urlencoded({extended:false}));

app.use(cors());



app.post('/mail',function(req,res){
    var d = req.body

    let transporter = nodemailer.createTransport({
        host: 'smtp.sohu.com',
        port: 465,
        secure: true,
        auth: {
            user: 'snailhr@sohu.com', //邮箱的账号
            pass: 'TP19MS27HNEAKI'//邮箱的密码
        }
    });

    let mailOptions = {
        from: 'snailhr@sohu.com', //邮件来源
        // to: 'edison@china-xiangsu.com', //邮件发送到哪里，多个邮箱使用逗号隔开
        to: '710321444@qq.com',
        subject: '【英文官网】来自 '+d.name+' 的咨询', // 邮件主题
        // text: 'Hello world ?', // 存文本类型的邮件正文
        // html: '<b>Hello world ?</b>' // html类型的邮件正文
        html:`<div style="width:1000px;margin:50px auto 0">
                <h4>姓名：${d.name}</h4>
                <h4>电话：${d.mobile}</h4>
                <h4>邮箱：${d.mail}</h4>
                <h4>留言信息：${d.text}</h4>
            </div>
        `
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            res.json({code:200,msg:'fail',data:'Message delivery failed. Please try again later'})
            return console.log(error);
        }
        console.log('Message %s sent: %s', info.messageId, info.response);
        res.json({code:200,msg:'success',data:'The email has been sent successfully, thank you for your letter, we will get in touch with you at the first time!'})
    });
})

app.listen(1313,function () {
    console.log('服务已启动');
})



